package com.imooc.course.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.google.common.collect.Lists;
import com.imooc.course.dao.CourseRepository;
import com.imooc.course.entity.Course;
import com.imooc.course.entity.UserInfo;
import com.imooc.course.service.ICourseService;
import com.imooc.dto.CourseDTO;
import com.imooc.dto.TeacherDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Auther: zhaozeyang
 * @Date: 2018/9/3 15:27
 * @Description:
 */
@Service
public class CourseServiceImpl implements ICourseService {


    @Autowired
    private CourseRepository courseRepository;

    @Override
    public List<CourseDTO> courseList() {
        List<Course> courseList = courseRepository.findAll();
        List<CourseDTO> result = Lists.newArrayList();
        if (courseList != null) {
            for (Course course : courseList) {
                UserInfo teacher = course.getTeacher();
                TeacherDTO teacherDTO = new TeacherDTO();
                BeanUtils.copyProperties(teacher, teacherDTO);
                CourseDTO courseDTO = new CourseDTO();
                BeanUtils.copyProperties(course, courseDTO);
                courseDTO.setTeacher(teacherDTO);
                result.add(courseDTO);
            }
        }
        return result;
    }
}
